package test.changeuser;


import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

import ru.esteru.selenium.factory.WebDriverFactory;

public class changeuser{
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	@Before
	public void setUp() throws Exception {
		driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
		baseUrl = "http://localhost/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void testchangeuser() throws Exception {
		goToMainPage();
		login("admin", "1");
		goToMyProfile();
		changePassword("1", "1");
		createUser("test@test.com",  "test", "1", "1");
		logout();
		login("test", "1");
		logout();
	
	}
	
	@Test
	public void testaddmovies() throws Exception {
		goToMainPage();
		login("admin", "1");
		goToaddMovie();
		addMovie("1", "test", "2012");
		logout();
	}

	private void addMovie(String imdNumber, String movieName, String year) {
		driver.findElement(By.name("imdbid")).clear();
		driver.findElement(By.name("imdbid")).sendKeys(imdNumber);
		driver.findElement(By.name("name")).clear();
		driver.findElement(By.name("name")).sendKeys(movieName);
		driver.findElement(By.name("year")).clear();
		driver.findElement(By.name("year")).sendKeys(year);
		driver.findElement(By.id("submit")).click();
	}

	private void goToaddMovie() {
		driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
	}

	private void logout() {
		driver.findElement(By.linkText("Log out")).click();
		driver.switchTo().alert().accept();
	}

	private void createUser(String email, String newUsername, String password, String password2) {
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(newUsername);
		driver.findElement(By.name("email")).clear();
		driver.findElement(By.name("email")).sendKeys(email);
		changePassword(password, password2);
	}

	private void changePassword(String password, String password2) {
		driver.findElement(By.id("password")).clear();
		driver.findElement(By.id("password")).sendKeys(password);
		driver.findElement(By.id("password2")).clear();
		driver.findElement(By.id("password2")).sendKeys(password2);
		driver.findElement(By.name("submit")).click();
	}

	private void goToMyProfile() {
		driver.findElement(By.linkText("My profile")).click();
	}

	private void login(String username, String password) {
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(username);
		driver.findElement(By.name("password")).clear();
		driver.findElement(By.name("password")).sendKeys(password);
		driver.findElement(By.name("submit")).click();
	}

	private void goToMainPage() {
		driver.get(baseUrl + "/php4dvd/");
	}

	@After
	public void tearDown() throws Exception {
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
